package cn.iocoder.yudao.module.xkfz.service.operationalPerformance.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.xkfz.domain.entity.operationalPerformance.AcademicMisconduct;
import cn.iocoder.yudao.module.xkfz.domain.entity.rules.ScoreCalculationData;
import cn.iocoder.yudao.module.xkfz.domain.excel.entity.AchievementAward;
import cn.iocoder.yudao.module.xkfz.domain.excel.entity.Standard;
import cn.iocoder.yudao.module.xkfz.domain.excel.po.AchievementAwardPo;
import cn.iocoder.yudao.module.xkfz.domain.excel.po.StandardPo;
import cn.iocoder.yudao.module.xkfz.domain.po.operationalPerformance.AcademicMisconductPo;
import cn.iocoder.yudao.module.xkfz.mapper.excel.AchievementAwardMapper;
import cn.iocoder.yudao.module.xkfz.mapper.excel.StandardMapper;
import cn.iocoder.yudao.module.xkfz.mapper.operationalPerformance.AcademicMisconductMapper;
import cn.iocoder.yudao.module.xkfz.mapper.rules.GroupTypeMapper;
import cn.iocoder.yudao.module.xkfz.service.excel.StandardService;
import cn.iocoder.yudao.module.xkfz.service.operationalPerformance.AcademicMisconductService;
import cn.iocoder.yudao.module.xkfz.service.reporting.ScoreCalculationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author ZWC
 * @Date 2024/5/14 16:05
 */
@Service
@Slf4j
public class AcademicMisconductImpl implements AcademicMisconductService {

    @Autowired
    private AcademicMisconductMapper academicMisconductMapper;

    @Autowired
    private GroupTypeMapper groupTypeMapper;

    @Autowired
    private ScoreCalculationService scoreCalculationService;

    private final String INDEX_ID = "42";

    /**
     * 功能描述: 计算某一年份的得分数据并插入到得分表中
     * @Param:
     * @Return: 返回插入得分表的条数
     * @Author: ZWC
     * @Date: 2024/5/20
     */
    private Integer addScoreData(String year){
        //合并两张表的查询数据
        List<ScoreCalculationData> countDataList = groupTypeMapper.getAcademicMisconductCount(year);
        Integer num = scoreCalculationService.selectionRule(INDEX_ID, year, countDataList);
        log.info("插入得分表数据条数{}，年份{}",num,year);
        return num;
    }

    @Override
    public PageResult<AcademicMisconduct> selectPage(AcademicMisconductPo reqVO) {
        return academicMisconductMapper.selectPage(reqVO);
    }

    @Override
    public Boolean insert(AcademicMisconduct req) {
        if (academicMisconductMapper.insert(req) > 0) {
            addScoreData(req.getPeriodTime());
        }
        return true;
    }

    @Override
    public Boolean update(AcademicMisconduct req) {
        if (academicMisconductMapper.updateById(req) > 0) {
            addScoreData(req.getPeriodTime());
        }
        return true;
    }


    @Override
    public Boolean delete(String id) {
        AcademicMisconduct academicMisconduct = academicMisconductMapper.selectById(id);
        if (academicMisconductMapper.deleteById(id) > 0) {
            addScoreData(academicMisconduct.getPeriodTime());
        }
        return true;
    }

}
